// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
let _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let  openid = wxContext.OPENID
  let province = event.province
  let city = event.city
  let district = event.district
  let count = await getCount(province,city,district);
 count = count.total;
let list = []
for (let i = 0; i < count; i += 100) {//自己设置每次获取数据的量
 list = list.concat(await getList(i));
}
return list;
}
async function getCount(province,city,district) {//获取数据的总数，这里记得设置集合的权限
let count = await db.collection('send-Goods').where(db.command.and([
    {
      province:db.RegExp({
       regexp:province,
       options:'i'
     })
    },
    {
      city:db.RegExp({
        regexp:city,
        options:'i'
      })
    },
    {
      district:db.RegExp({
        regexp:district,
        options:'i'
      })
    },
  ])).count();
return count;
}
async function getList(skip) {//分段获取数据
let list = await db.collection('send-Goods')
.skip(skip).get();
return list.data;
}
